home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / ksubr.c < prev    next >
C/C++ Source or Header  |  1992-05-14  |  5KB  |  241 lines

  1. /* @(#) $Header: ksubr.c,v 1.6 92/05/14 13:20:12 deyke Exp $ */
  2.  
  3. /* Machine or compiler-dependent portions of kernel
  4.  *
  5.  * Copyright 1991 Phil Karn, KA9Q
  6.  */
  7. #include <sys/types.h>
  8. #include <stdio.h>
  9. #include <time.h>
  10. /* #include <dos.h> */
  11. #include "global.h"
  12. #include "proc.h"
  13. /* #include "pc.h" */
  14. #include "commands.h"
  15.  
  16. static oldNull;
  17.  
  18. #ifdef __TURBOC__
  19. /* Template for contents of jmp_buf in Turbo C */
  20. struct env {
  21.     unsigned        sp;
  22.     unsigned        ss;
  23.     unsigned        flag;
  24.     unsigned        cs;
  25.     unsigned        ip;
  26.     unsigned        bp;
  27.     unsigned        di;
  28.     unsigned        es;
  29.     unsigned        si;
  30.     unsigned        ds;
  31. };
  32. #define getstackptr(ep) (ptol(MK_FP((ep)->ss, (ep)->sp)))
  33. #else
  34. #ifdef __hp9000s300
  35. struct env {
  36.     long    pc;
  37.     long    d2;
  38.     long    d3;
  39.     long    d4;
  40.     long    d5;
  41.     long    d6;
  42.     long    d7;
  43.     long    a2;
  44.     long    a3;
  45.     long    a4;
  46.     long    a5;
  47.     long    a6;
  48.     long    a7;
  49. };
  50. #define getstackptr(ep) ((ep)->a7)
  51. #else
  52. #ifdef __hp9000s800
  53. struct env {
  54.     long    i_dont_know;
  55.     long    sp;
  56. };
  57. #define getstackptr(ep) ((ep)->sp)
  58. #else
  59. #ifdef ISC
  60. struct env {
  61.     long    esp;
  62.     long    ebp;
  63.     long    eax;
  64.     long    ebx;
  65.     long    ecx;
  66.     long    edx;
  67.     long    edi;
  68.     long    esi;
  69.     long    eip;
  70.     long    flag;
  71. };
  72. #define getstackptr(ep) ((ep)->esp)
  73. #else
  74. struct env {
  75.     long    dummy;
  76. };
  77. #define getstackptr(ep) (0L)
  78. #endif
  79. #endif
  80. #endif
  81. #endif
  82.  
  83. static int stkutil __ARGS((struct proc *pp));
  84.  
  85. void
  86. kinit()
  87. {
  88.     /* Remember location 0 pattern to detect null pointer derefs */
  89.     oldNull = *(unsigned short *)NULL;
  90.  
  91. }
  92. /* Print process table info
  93.  * Since things can change while ps is running, the ready proceses are
  94.  * displayed last. This is because an interrupt can make a process ready,
  95.  * but a ready process won't spontaneously become unready. Therefore a
  96.  * process that changes during ps may show up twice, but this is better
  97.  * than not having it showing up at all.
  98.  */
  99. int
  100. ps(argc,argv,p)
  101. int argc;
  102. char *argv[];
  103. void *p;
  104. {
  105.     register struct proc *pp;
  106.     register struct env *ep;
  107.     int i;
  108.  
  109.     extern time_t StartTime;
  110.  
  111.     printf("Uptime %s",tformat(secclock()-StartTime));
  112.     printf("\n");
  113.  
  114.     printf("PID       SP        stksize   maxstk    event     fl  in  out  name\n");
  115.  
  116.     for(pp = Susptab;pp != NULLPROC;pp = pp->next){
  117.         ep = (struct env *)&pp->env;
  118.         if(printf("%08lx  %08lx  %-10u%-10u%08lx  %c%c%c %3d %3d  %s\n",
  119.          ptol(pp),
  120.          getstackptr(ep),
  121.          pp->stksize,
  122.          stkutil(pp),
  123.          ptol(pp->event),
  124.          pp->i_state ? 'I' : ' ',
  125.          (pp->state & WAITING) ? 'W' : ' ',
  126.          (pp->state & SUSPEND) ? 'S' : ' ',
  127.          pp->input, pp->output,
  128.          pp->name) == EOF)
  129.             return 0;
  130.     }
  131.     for(i=0;i<PHASH;i++){
  132.         for(pp = Waittab[i];pp != NULLPROC;pp = pp->next){
  133.             ep = (struct env *)&pp->env;
  134.             if(printf("%08lx  %08lx  %-10u%-10u%08lx  %c%c%c %3d %3d  %s\n",
  135.              ptol(pp),getstackptr(ep),pp->stksize,stkutil(pp),
  136.              ptol(pp->event),
  137.              pp->i_state ? 'I' : ' ',
  138.              (pp->state & WAITING) ? 'W' : ' ',
  139.              (pp->state & SUSPEND) ? 'S' : ' ',
  140.              pp->input,pp->output,
  141.              pp->name) == EOF)
  142.                 return 0;
  143.         }
  144.     }
  145.     for(pp = Rdytab;pp != NULLPROC;pp = pp->next){
  146.         ep = (struct env *)&pp->env;
  147.         if(printf("%08lx  %08lx  %-10u%-10u          %c%c%c %3d %3d  %s\n",
  148.          ptol(pp),getstackptr(ep),pp->stksize,stkutil(pp),
  149.          pp->i_state ? 'I' : ' ',
  150.          (pp->state & WAITING) ? 'W' : ' ',
  151.          (pp->state & SUSPEND) ? 'S' : ' ',
  152.          pp->input,pp->output,
  153.          pp->name) == EOF)
  154.             return 0;
  155.     }
  156.     if(Curproc != NULLPROC){
  157.         ep = (struct env *)&Curproc->env;
  158.         printf("%08lx  %08lx  %-10u%-10u          %c   %3d %3d  %s\n",
  159.          ptol(Curproc),getstackptr(ep),Curproc->stksize,
  160.          stkutil(Curproc),
  161.          Curproc->i_state ? 'I' : ' ',
  162.          Curproc->input,Curproc->output,
  163.          Curproc->name);
  164.     }
  165.     return 0;
  166. }
  167. static int
  168. stkutil(pp)
  169. struct proc *pp;
  170. {
  171.     unsigned i;
  172.     register int16 *sp;
  173.  
  174.     if(pp->stksize == 0)
  175.         return 0;       /* Main task -- too hard to check */
  176.     i = pp->stksize;
  177. #ifndef __hp9000s800
  178.     for(sp = pp->stack;*sp == STACKPAT && sp < pp->stack + pp->stksize;sp++)
  179. #else
  180.     for(sp = pp->stack + (pp->stksize-1);*sp == STACKPAT && sp >= pp->stack;sp--)
  181. #endif
  182.         i--;
  183.     return i;
  184. }
  185.  
  186. /* Verify that stack pointer for current process is within legal limits;
  187.  * also check that no one has dereferenced a null pointer
  188.  */
  189. void
  190. chkstk()
  191. {
  192.     int16 *sbase;
  193.     int16 *stop;
  194.     int16 *sp;
  195.  
  196. #ifdef __TURBOC__
  197.     sp = MK_FP(_SS,_SP);
  198.     if(_SS == _DS){
  199.         /* Probably in interrupt context */
  200.         return;
  201.     }
  202. #else
  203.     sp = (int16 *) &sp;
  204. #endif
  205.     sbase = Curproc->stack;
  206.     if(sbase == NULL)
  207.         return; /* Main task -- too hard to check */
  208.  
  209.     stop = sbase + Curproc->stksize;
  210.     if(sp < sbase || sp >= stop){
  211.         printf("Stack violation, process %s\n",Curproc->name);
  212.         printf("SP = %lx, legal stack range [%lx,%lx)\n",
  213.         ptol(sp),ptol(sbase),ptol(stop));
  214.         fflush(stdout);
  215.         killself();
  216.     }
  217.     if(*(unsigned short *)NULL != oldNull){
  218.         printf("WARNING: Location 0 smashed, process %s\n",Curproc->name);
  219.         *(unsigned short *)NULL = oldNull;
  220.         fflush(stdout);
  221.     }
  222. }
  223. unsigned
  224. phash(event)
  225. void *event;
  226. {
  227.     register unsigned x;
  228.  
  229.     /* Fold the two halves of the pointer */
  230. #ifdef __TURBOC__
  231.     x = FP_SEG(event) ^ FP_OFF(event);
  232. #else
  233.     x = (((int) event >> 16) ^ (int) event) & 0xffff;
  234. #endif
  235.  
  236.     /* If PHASH is a power of two, this will simply mask off the
  237.      * higher order bits
  238.      */
  239.     return x % PHASH;
  240. }
  241.